package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.TransactionCasesEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishServiceDealedCasesActivityModel extends AndroidViewModel {
    public MutableLiveData<List<TransactionCasesEntity>> ldCaseList = new MutableLiveData<>();
    public MutableLiveData<String> ldCaseDelete = new MutableLiveData<>();
    private int pageNum = 1;

    public PublishServiceDealedCasesActivityModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取发布服务列表
     */
    public void getTransactionList(Context context, boolean isLoadMore, String id) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTransactionList(id, 20, pageNum).enqueue(new Callback<BaseEntity<ListResponse<List<TransactionCasesEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> call, Response<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldCaseList.postValue(response.body().getData().getList());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TransactionCasesEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取发布服务列表
     */
    public void deleteTransaction(Context context, String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().deleteTransactionCases(id).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldCaseDelete.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
